from builtins import range
import numpy as np
from random import shuffle
from past.builtins import xrange

def softmax_loss_naive(W, X, y, reg):
    """
    Softmax loss function, naive implementation (with loops)

    Inputs have dimension D, there are C classes, and we operate on minibatches
    of N examples.

    Inputs:
    - W: A numpy array of shape (D, C) containing weights.
    - X: A numpy array of shape (N, D) containing a minibatch of data.
    - y: A numpy array of shape (N,) containing training labels; y[i] = c means
      that X[i] has label c, where 0 <= c < C.
    - reg: (float) regularization strength

    Returns a tuple of:
    - loss as single float
    - gradient with respect to weights W; an array of same shape as W
    """
    # Initialize the loss and gradient to zero.
    loss = 0.0
    dW = np.zeros_like(W)
    num_train = X.shape[0]
    num_classes = W.shape[1]
    D = W.shape[0]
    
    #############################################################################
    # TODO: Compute the softmax loss and its gradient using explicit loops.     #
    # Store the loss in loss and the gradient in dW. If you are not careful     #
    # here, it is easy to run into numeric instability. Don't forget the        #
    # regularization!                                                           #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    
    for i in range(num_train):
        scores = np.exp(X[i].dot(W)) # calculate scores and exp scores
        correct_class_score = scores[y[i]]
        P = correct_class_score / np.sum(scores)
        loss += - np.log(P)
        
        # Calculate gradient array
        # p_score_p_W means partial_score(k) / partial W(m, n)
        for j in range(num_classes):
            if j == y[i]: # For correct catagory
                dW[:, j] += - 1 / np.sum(scores) * (np.sum(scores) - correct_class_score) * X[i]
            else: # For non-correct catagory. Here we calculate one column once
                dW[:, j] += 1 / np.sum(scores) * scores[j] * X[i]
            
    # Right now the loss is a sum over all training examples, but we want it
    # to be an average instead so we divide by num_train.
    loss /= num_train
    dW /= num_train
    
    # Add regularization to the loss.
    loss += reg * np.sum(W * W)
    dW += 2 * reg * W
    
    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    return loss, dW


def softmax_loss_vectorized(W, X, y, reg):
    """
    Softmax loss function, vectorized version.

    Inputs and outputs are the same as softmax_loss_naive.
    """
    # Initialize the loss and gradient to zero.
    loss = 0.0
    dW = np.zeros_like(W)
    num_classes = W.shape[1]
    num_train = X.shape[0]
    
    #############################################################################
    # TODO: Compute the softmax loss and its gradient using no explicit loops.  #
    # Store the loss in loss and the gradient in dW. If you are not careful     #
    # here, it is easy to run into numeric instability. Don't forget the        #
    # regularization!                                                           #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    
    # Loss
    scores = np.exp(X.dot(W)) # size = (N, C)
    cor_score = scores[list(range(num_train)), y]
    P = cor_score / np.sum(scores, axis=1)
    loss += np.sum(- np.log(P))
    
    # Gradient
    X_mask = np.zeros((num_train, num_classes))
    X_mask[np.arange(num_train), y] -= 1
    X_mask += (scores / np.sum(scores, axis=1, keepdims=True))
    
    temp = X.T.dot(X_mask)
    
    dW += np.sum(temp, axis=0)
    
    # We calculate mean loss
    loss /= num_train
    dW /= num_train
    
    # Add regularization to the loss.
    loss += reg * np.sum(W * W)
    dW += 2 * reg * W
    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    
    return loss, dW
